/**
 * @description 在途回购信息
 * @author wangjianjun
 */

import React, { PureComponent } from 'react';
import { TableC } from '../../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Modal } from 'antd';
import style from './index.less';
const confirm = Modal.confirm;
import { connect } from 'dva';
import 'moment/locale/zh-cn';

@Form.create()
@connect(({ exchangeAgreement, bondLendingM, userM: user, loading }) => ({
  exchangeAgreement,
  bondLendingM,
  user,
  loading: loading.effects['bondLendingM/getBuyBack'],
}))
export default class RepurchaseInfor extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      columns: [],
      dataSource: [],
      rowData: null,
    };
  }

  componentDidMount() {}

  UNSAFE_componentWillReceiveProps(nextProps) {
    const init = !this.props.RepurchaseInfor ? '' : JSON.stringify(this.props.RepurchaseInfor);
    const initN = !nextProps.RepurchaseInfor ? '' : JSON.stringify(nextProps.RepurchaseInfor);
    if (init != initN) {
      this.setState(
        {
          search: nextProps.RepurchaseInfor,
        },
        () => {
          if (nextProps.RepurchaseInfor != null) {
            this.getTableData();
          } else {
            this.setState({
              columns: [],
              dataSource: [],
            });
          }
        },
      );
    }
  }

  // 获取在途数据
  getTableData = () => {
    const { search } = this.state;
    if (search) {
      this.props.dispatch({
        type: 'bondLendingM/getBuyBack',
        payload: {
          ...search,
          currentPage: 1,
          numInPage: 500,
        },
        callback: (data) => {
          if (data) {
            this.setState({
              columns: data.columns,
              dataSource: data.dataSource,
            });
          }
        },
      });
    }
  };

  render() {
    const { columns, dataSource } = this.state;

    if (columns && columns.length > 0) {
      columns.map((item, key) => {
        if (item.key == 'transactionDate') {
          item.width = '10%';
        }
        if (item.key == 'pledgebondName') {
          item.width = '10%';
        }
        if (item.key == 'loanFaceValue') {
          item.width = '10%';
          item.type = 'money';
        }
        if (item.key == 'pledgeAmount') {
          item.width = '10%';
          item.type = 'number';
        }
        if (item.key == 'lendingDays') {
          item.width = '10%';
          item.type = 'number';
        }

        if (item.key == 'firstsettlementDate') {
          item.width = '10%';
        }
        if (item.key == 'settlementDate') {
          item.width = '10%';
        }
        if (item.key == 'lendingRate') {
          item.type = 'number';
          item.toFixed = '4';
          item.width = '10%';
        }
        if (item.key == 'lendingFee') {
          item.type = 'money';
          item.width = '10%';
        }
        if (item.key == 'counterParty') {
          item.width = '10%';
        }
      });
    }

    return (
      <div>
        <TableC
        noDataHeight="110px"
          columns={this.state.columns}
          dataSource={this.state.dataSource}
          loading={this.props.loading}
          onRow={(record) => {
            return {
              onDoubleClick: (event) => {
                this.setState(
                  {
                    rowData: record,
                  },
                  () => {
                    if (this.props.type == '1') {
                      this.props.getConfigData('backfillData', record);
                    }
                  },
                );
              },
            };
          }}
          rowClassName={(record) => {
            return this.state.rowData && record.id == this.state.rowData.id ? style.row_select : '';
          }}
          hidePage={true}
          scroll={{ x: 1400, y: 350 }}
        />
      </div>
    );
  }
}
